﻿using System;
using System.Net;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    [AutomaticTemplate]
    public class MessageController : Controller
    {
        public ActionResult Index(string containerId)
        {
            var vm = new MessageIndexModel();
            vm.LeftTree = new Node()
            {
                Text = ".",
                NodeID = "Root"
            };
            vm.LeftTree.Children.Add(new Node()
            {
                Text = "收件箱",
                Expanded = false,
                AttributesObject = new
                {
                    type = "Inbox"
                }
            });
            vm.LeftTree.Children.Add(new Node()
            {
                Text = "发件箱",
                Expanded = false,
                AttributesObject = new
                {
                    type = "Outbox"
                }
            });
            vm.LeftTree.Children.Add(new Node()
            {
                Text = "草稿箱",
                Expanded = false,
                AttributesObject = new
                {
                    type = "Drafts"
                }
            });
            vm.LeftTree.Children.Add(new Node()
            {
                Text = "回收站",
                Expanded = false,
                AttributesObject = new
                {
                    type = "RecycleBin"
                }
            });

            return new PartialViewResult
            {
                ViewName = "Index",
                Model = vm,
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }
        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessagePaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }
        /// <summary>
        /// 发件箱
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetOutboxPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessageOutboxPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        /// <summary>
        /// 草稿箱
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetDraftboxPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessageDraftboxPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        /// <summary>
        /// 垃圾箱
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetDustbinPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessageDustbinPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MessageDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                dto = proxy.Channel.GetMessageInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            return new PartialViewResult { Model = dto.To<MessageModel>() };
        }


        public ActionResult Send()
        {
            return new PartialViewResult { ViewName="Send", Model = new MessageModel() };
        }


        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MessageDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                dto = proxy.Channel.GetMessageInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }


            return new PartialViewResult { Model = dto.To<MessageModel>() };
        }


        [System.Web.Mvc.HttpPost]
        public ActionResult Save(MessageModel user)
        {
            MessageDTO dto = user.To<MessageDTO>();

            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var res = proxy.Channel.SendMessage(dto);
                return this.JsonNet(res);
            }
        }

        [System.Web.Mvc.HttpPost]
        public ActionResult Delete(int[] ids)
        {
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                res = proxy.Channel.DeleteMessages(ids);
            }

            return this.JsonNet(res);
        }
    }
}